<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Oppo
 *
 * @author sunday add 2014.5.28 | dragon.hu 2015.11.27 modify
 */
require_once 'BaseChannel.php';

//oppo
class Oppo implements BaseChannel {
	
	function __construct() {
		//echo "construct";
	}

	//put your code here
	function android($data) {
		//$paymentKey="NIhmYdfPe05f";  貌似不需要这个key
		$paymentKey="";
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) {
		//$paymentKey="NIhmYdfPe05f";
		$paymentKey="";
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		//require_once 'include/oppo/notify.class.php';
		
		$orderLog = new ChargeOrder();
		$orderLog->log("oppo", $_REQUEST);

		$notifyId = $_REQUEST['notifyId'];	//回调通知 ID（该值使用系统为这次支付生成的订单号） 
		$partnerOrder = $_REQUEST['partnerOrder'];	//开发者订单号（客户端上传） 
		$productName = $_REQUEST['productName'];	//商品名称（客户端上传） 
		$productDesc = $_REQUEST['productDesc'];	//商品描述（客户端上传）
		$price = $_REQUEST['price'];	//单位 是分
		$count = $_REQUEST['count'];	//商品数量（一般为1） 
		$attach = $_REQUEST['attach'];	//请求支付时上传的附加参数（客户端上传）
		
		//$str_contents= "notifyId={$notifyId}&partnerOrder={$partnerOrder}&productName={$productName}
		//		&productDesc={$productDesc}&price={$price}&count={$count}&attach={$attach}";
		/*返回 结果
		 * result=arg0&resultMsg=arg1 
		    arg0：值为“OK”或“FAIL”。两者选其一。该值为必填字段 
		    arg1：arg0为“OK”时该值可以为空字符串，arg0为“FAIL”时建议提供些
			有意义的信息，便于查找问题，该值非强制字段。 
			例如：  result=OK&resultMsg=成功 
		           result=FAIL&resultMsg=网络原因发货失败 
		 */
		//验证签名
		if( $this->rsa_verify($_REQUEST) )
		{
			$status = STATUS_SUCCESS;
			//收到 的钱是分为单位。。
			if(true == $orderLog->recordOrder($status, $price/100, $partnerOrder, "oppo", $platform,$notifyId))
			{
				$orderLog->log('oppo','platform:'.$platform.'amount:'.($price/100).' order_id:'.$partnerOrder.' success');
				$ret = "result=OK&resultMsg=success";
	        }else 
	        { 			
				$orderLog->log('oppo','platform:'.$platform.'amount:'.($price/100).' order_id:'.$partnerOrder.' fail');
				$ret = "result=FAIL&resultMsg=insertSqlError";
	        }
		} else 
		{
			//echo " order:".$json['order_id_com']." sign verify fail";
			$orderLog->log('oppo'," order:".$partnerOrder." sign verify fail");
			$ret = "result=FAIL&resultMsg=signNotMatch";
		}	
		return $ret;		
	}	
	
	/*function rsa_verify($contents) 
	{
		$str_contents = "notifyId={$contents['notifyId']}&partnerOrder={$contents['partnerOrder']}&productName={$contents['productName']}&productDesc={$contents['productDesc']}&price={$contents['price']}&count={$contents['count']}&attach={$contents['attach']}";
		$publickey= 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCmreYIkPwVovKR8rLHWlFVw7YDfm9uQOJKL89Smt6ypXGVdrAKKl0wNYc3/jecAoPi2ylChfa2iRu5gunJyNmpWZzlCNRIau55fxGW0XEu553IiprOZcaw5OuYGlf60ga8QT6qToP0/dpiL/ZbmNUO9kUhosIjEu22uFgR+5cYyQIDAQAB';
		$pem = chunk_split($publickey,64,"\n");
		$pem = "-----BEGIN PUBLIC KEY-----\n".$pem."-----END PUBLIC KEY-----\n";
		$public_key_id = openssl_pkey_get_public($pem);
		$signature =base64_decode($contents['sign']);
		return openssl_verify($str_contents, $signature, $public_key_id );
	}*/
	
	//modify by dragonhu @2015.11.27 new oppo
	function rsa_verify($contents) {
		$str_contents = "notifyId={$contents['notifyId']}&partnerOrder={$contents['partnerOrder']}&productName={$contents['productName']}&productDesc={$contents['productDesc']}&price={$contents['price']}&count={$contents['count']}&attach={$contents['attach']}";
		$publickey= 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCmreYIkPwVovKR8rLHWlFVw7YDfm9uQOJKL89Smt6ypXGVdrAKKl0wNYc3/jecAoPi2ylChfa2iRu5gunJyNmpWZzlCNRIau55fxGW0XEu553IiprOZcaw5OuYGlf60ga8QT6qToP0/dpiL/ZbmNUO9kUhosIjEu22uFgR+5cYyQIDAQAB';
		$pem = chunk_split($publickey,64,"\n");
		$pem = "-----BEGIN PUBLIC KEY-----\n".$pem."-----END PUBLIC KEY-----\n";
		$public_key_id = openssl_pkey_get_public($pem);
		$signature =base64_decode($contents['sign']);
		return openssl_verify($str_contents, $signature, $public_key_id );//成功返回1,0失败，-1错误,其他看手册
	}
}